package com.linqi.freebie.utils

import com.linqi.freebie.config.DepotInfo
import java.io.File

object SteamTools {

    suspend fun setup(
        depotData: List<DepotInfo>,
        appId: String,
        steamPath: File,
        depotMap: Map<String, List<String>>? = null,
        versionLock: Boolean = false
    ): Boolean {
        return try {
            // 创建目录: steamPath/config/stplug-in
            val stPath = File(steamPath, "config/stplug-in")
            if (!stPath.exists()) {
                stPath.mkdirs()
            }

            // Lua 脚本内容
            val stringBuilder = StringBuilder()
            stringBuilder.append("addappid($appId, 1, \"None\")\n")

            // 遍历 depotData 生成 Lua 脚本
            for (depot in depotData) {
                if (versionLock && depotMap?.containsKey(depot.depotId) == true) {
                    depotMap[depot.depotId]?.forEach { manifestId ->
                        stringBuilder.append("addappid(${depot.depotId}, 1, \"${depot.decryptionKey}\")\n")
                        stringBuilder.append("setManifestid(${depot.depotId},\"$manifestId\")\n")
                    }
                } else {
                    stringBuilder.append("addappid(${depot.depotId}, 1, \"${depot.decryptionKey}\")\n")
                }
            }

            // 保存 Lua 文件
            val luaFile = File(stPath, "$appId.lua")
            luaFile.writeText(stringBuilder.toString(), Charsets.UTF_8)

            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
}

// 数据类
